<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;


/**
* 商品型号
*/
class VersionController extends Controller
{
    /**
     * 显示当前商品下的商品型号
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request, $gid)
    {
        //闪存信息
        $request -> flash();

        //接收分页 搜索数据
        $title = $request -> input('search','');
        $count = $request -> input('count',10);
        
        //接收参数数组
        $var = [];
        $var['title'] = $title;
        $var['count'] = $count;

        //查询数据库
        $data = DB::table('goods_version')
            -> where('title', 'like', '%'.$title.'%')
            -> where('gid', $gid)
            -> paginate($count);

        //查询商品名称
        $gtitle = DB::table('goods') -> where('id', $gid) -> first();
        $title = $gtitle['title'];
        
        //解析模板
        return view('admin/version/index',['data' => $data, 'gid' => $gid, 'var' => $var, 'title' => $title]);
    }

    /**
     * 新增商品型号
     *
     * @return \Illuminate\Http\Response
     */
    public function postInsert(Request $request, $gid)
    {
        // 检测数据
        $data = $request -> except('_token');
        $data['ctime'] = time();
        $data['gid'] = $gid;
       
        //插入数据库
        $res = DB::table('goods_version') -> insert($data);

        //返回结果
        return self::res($res,'添加型号','/admin/version/index/'.$gid,'/admin/version/index/'.$gid);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function getEdit(Request $request, $gid)
    {
        //接收数据
        $id = $request -> input('id');

        $data = DB::table('goods_version')
            -> where('id', $id )
            -> first();

        //返回编辑模板
        return view('admin/version/edit', ['data' => $data, 'gid' => $gid]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getDown(Request $request, $gid)
    {
        //
        $id = $request -> input('id');

        $res = DB::table('goods_version') -> where('id', $id) -> delete();

        //返回结果
        return self::res($res,'删除型号','/admin/version/index/'.$gid,'/admin/version/index/'.$gid);
    }

    /**
     * 新增型号
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function getAdd($gid)
    {
        //返回增加界面
        return view('admin/version/add', ['gid' => $gid]);
    }

    /**
     * 更新商品型号
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request, $gid)
    {
        //
        $id = $request -> input('id');
            
        //过滤数据
        $receive = $request -> except('_token');
        
        //去除空值
        $data = [];
        foreach ($receive as $k => $v) {
            if (!empty($v)) {
                $data[$k] = $v;
            }
        }

        //更新数据
        $res = DB::table('goods_version') -> where('id', $id) -> update($data);
   
        //结果
        return self::res($res,'修改','/admin/version/edit/'.$gid.'?id='.$id,'/admin/version/edit/'.$gid.'?id='.$id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }


     /**
    * 处理结果函数
    */
     static public function res($res,$message,$url_su,$url_er){
        if ($res) {
         
           //跳转到员工列表并显示成功信息
           return redirect($url_su)
                        ->with(['message'=>$message.'成功','type'=>1]);
        }else{

           //跳转到添加页面
           return redirect($url_er)
                        ->with(['message'=>$message.'失败','type'=>2]);
        }
    }
}
